package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import service.Service;

@SuppressWarnings("serial")
public class KnotenEingabeFrame extends JFrame {

  public KnotenEingabeFrame() {
    init();

  }

  public void init() {

    initFrame();

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(300, 150));
    setTitle("Graphentheorie - Stranz");
    setLocationByPlatform(true);
    pack();
    setVisible(true);

  }

  private void initFrame() {

    JPanel panel = new JPanel(new BorderLayout());
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();

    JLabel ueberschrift = new JLabel("Graphentheorie", JLabel.CENTER);
    ueberschrift.setFont(new Font("Serif", Font.BOLD, 20));

    final JTextField knotenanzahl = new JTextField();
    knotenanzahl.setColumns(2);
    knotenanzahl.addKeyListener(new KeyListener() {
      
      @Override
      public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
      }
      
      @Override
      public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
      }
      
      @Override
      public void keyPressed(KeyEvent arg0) {
          
        if(knotenanzahl.getText().length() >1)
          knotenanzahl.setText(knotenanzahl.getText().substring(0, 1));
           
        
      }
    });
    
    JLabel knotenanzahltext = new JLabel("Knotenanzahl eingeben: ", JLabel.CENTER);

    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {

        try {
          Service.getInstanz().setKnotenanzahl(Integer.parseInt(knotenanzahl.getText()));

        } catch (Exception e2) {
          JOptionPane.showMessageDialog(KnotenEingabeFrame.this, "Bitte Zahl zwischen 2 und 15 eingeben!");
          return;
        }

        Hauptfenster hauptfenster = new Hauptfenster();
        Service.getInstanz().setHauptfenster(hauptfenster);
        
        dispose();

      }
    });

    panel2.add(knotenanzahltext);
    panel3.add(ok);
    panel.add(ueberschrift, BorderLayout.NORTH);
    panel2.add(knotenanzahl);
    panel.add(panel2, BorderLayout.CENTER);
    panel.add(panel3, BorderLayout.SOUTH);
    add(panel);

  }

  public static void main(String[] args) {
    KnotenEingabeFrame kef = new KnotenEingabeFrame();

  }

}
